package com.j2eefast.framework.code.controller;

import com.j2eefast.framework.code.entity.SerialEntity;
import com.j2eefast.framework.code.service.SerialService;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>序列号管理</p>
 *
 * @author: summer
 * @date: 2021-05-29 22:03
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/code/serial")
public class SerialController extends BaseController {
	private String urlPrefix = "modules/code/serial";
	@Autowired
	private SerialService serialService;

	@RequiresPermissions("code:serial:view")
	@GetMapping()
	public String process() {
		System.out.println("11111111111111111");
		return urlPrefix + "/index";
	}
	@RequestMapping("/list")
	@RequiresPermissions("code:serial:list")
	@ResponseBody
	public ResponseData list(@RequestParam Map<String, Object> params) {
		PageUtil page = serialService.findPage(params);
		return success(page);
	}

	/**
	 * 删除
	 */
	@BussinessLog(title = "删除", businessType = BusinessType.DELETE)
	@RequestMapping( value = "/del", method = RequestMethod.POST)
	@RequiresPermissions("code:serial:del")
	@RequiresRoles(Constant.SU_ADMIN)
	@ResponseBody
	public ResponseData delete(Long[] ids) {
		return serialService.deleteByIds(ids)?success(): error("删除失败!");
	}


	/**
	 * 修改出入库 页面
	 */
	@RequiresPermissions("code:serial:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		SerialEntity pmsSerialEntity = serialService.getById(id);
		mmap.put("serial", pmsSerialEntity);
		return urlPrefix + "/edit";
	}

	/**
	 * 修改出入库保存
	 */
	@RepeatSubmit
	@RequiresPermissions("code:serial:edit")
	@BussinessLog(title = "修改", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData edit(SerialEntity pmsSerialEntity) {
		ValidatorUtil.validateEntity(pmsSerialEntity);
		return serialService.updateById(pmsSerialEntity) ? success() : error("修改失败!");
	}

	/**
	 * 新增参数配置
	 */
	@RequiresPermissions("code:serial:add")
	@GetMapping("/add")
	public String add() {
		return urlPrefix + "/add";
	}



	/**
	 * 新增保存
	 */
	@RepeatSubmit
	@RequiresPermissions("code:serial:add")
	@BussinessLog(title = "认领", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData addSave(@Validated SerialEntity pmsSerialEntity) {
		//校验参数
		ValidatorUtil.validateEntity(pmsSerialEntity);
		Long userId= UserUtils.getUserInfo().getId();
		//SysUserEntity sysUserEntity=pmsProjectService.getSysById(userId);
//		pmsProjectEntity.setRegisterName(sysUserEntity.getName());
		return serialService.save(pmsSerialEntity) ? success() : error("新增失败!");
	}

	/**
	 * 修改出入库保存
	 */

	@RequiresPermissions("code:serial:checkout")
	@BussinessLog(title = "出库", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/checkout", method = RequestMethod.POST)
	@ResponseBody
//	@GetMapping("/checkout/{id}")
	public ResponseData checkout(String id, ModelMap mmap) {

		ValidatorUtil.validateEntity(id);
		Long ids=Long.parseLong(id);
		return serialService.updateCheck(ids) ? success() : error("修改失败!");
	}






}
